import plugin from '../../../lib/plugins/plugin.js'
import fs from 'fs'
import { Data , Config } from '../components/index.js'
export class xy_addhelp extends plugin {
  constructor() {
    super({
      name: '小月附加帮助',
      event: 'message',
      priority: 1,
      rule: [
        {
          reg: '^#?附加帮助(添加|删除)菜单',
          fnc: 'add_help'
        },
        {
          reg: '^#?附加帮助菜单(\\d+)(添加|删除)功能',
          fnc: 'adduse_help'
        },
        {
          reg: '^#?删除附加帮助$',
          fnc: 'del_help'
        },
        {
          reg: '^#?附加帮助修改标题',
          fnc: 'set_help'
        },
      ]
    });
    this.path = process.cwd() + '/plugins/xiaoyue-plugin/config/addhelp.js'
  }

  async add_help(e) {
    if (!e.isMaster) return false
    try {
    let { diyCfg, sysCfg } = await Data.importCfg('addhelp')
    let addhelpList = diyCfg.helpList || sysCfg.helpList
    let addhelpCfg = diyCfg.helpCfg || sysCfg.helpCfg
    let biaoti = e.msg.match(/菜单.*/)?.[0].replace(/菜单/g, '').trim()
    if (!biaoti) return e.reply('菜单名呢？')
    if (/删除/.test(e.msg)) {
      let id = e.msg.match(/\d+/)?.[0]
      if (!id) return e.reply('删除菜单几呢？')
      if (!addhelpList[Number(id) - 1]) return e.reply('没有此菜单')
      addhelpList.splice(Number(id) - 1,1)
    } else {
    let data = {
      group: biaoti,
      list: []
    }
    addhelpList.push(data)
  }
    let helpCfg = `export const helpCfg = ${JSON.stringify(addhelpCfg ,null,'\t')}`
    let helpList = `\nexport const helpList = ${JSON.stringify(addhelpList,null,'\t')}`
    let add = helpCfg + helpList
    fs.writeFileSync(this.path,(add),'utf-8')
    e.reply(`${e.msg.includes('删除') ? '删除' : '添加'}成功`)
    } catch (err) {
    logger.error(err)
    e.reply(`${e.msg.includes('删除') ? '删除' : '添加'}成功`)
    }
  }
   async adduse_help(e) {
    if (!e.isMaster) return false
    try {
    let { diyCfg, sysCfg } = await Data.importCfg('addhelp')
    let addhelpCfg = diyCfg.helpCfg || sysCfg.helpCfg
    let helpCfg = `export const helpCfg = ${JSON.stringify(addhelpCfg ,null,'\t')}`
    let id = e.msg.match(/\d+/)?.[0]
    id = Number(id)
    let addhelpList = diyCfg.helpList || sysCfg.helpList
    if (id < 1 || id > addhelpList.length) return e.reply('请输入正确的菜单序列号')
    let gn = e.msg.match(/功能(.*?)，/g)?.[0].replace(/功能|，/g, '').trim()
    let ms = e.msg.match(/描述.*/g)?.[0].replace(/描述/g, '').trim()
    if (/删除/.test(e.msg)) {
      let gnid = e.msg.match(/功能.*/g)?.[0].replace(/功能/g, '').trim()
      if (!addhelpList[id - 1].list[Number(gnid) - 1]) return e.reply('请输入正确的功能序列号')
      addhelpList[id - 1].list.splice(Number(gnid) - 1,1)
    } else {
    let data = {
      icon : 7,
      title: gn,
      desc: ms
    }
    addhelpList[id - 1].list.push(data)
  }
    let helpList = `\nexport const helpList = ${JSON.stringify(addhelpList,null,'\t')}`
    let add = helpCfg + helpList
    fs.writeFileSync(this.path,(add),'utf-8')
    e.reply(`${e.msg.includes('删除') ? '删除' : '添加'}成功`)
  } catch(err) {
    logger.error(err)
    e.reply(`${e.msg.includes('删除') ? '删除' : '添加'}成功`)
  }
 }

 async del_help(e) {
  if (!e.isMaster) return false
  fs.unlink(this.path, (err) =>{
    if (err) {
      logger.warn(err)
      e.reply('删除失败')
      return false
    }
    e.reply('删除成功')
  })
 }
 async set_help(e) {
  if (!e.isMaster) return false
  try {
  let { diyCfg, sysCfg } = await Data.importCfg('addhelp')
  let addhelpList = diyCfg.helpList || sysCfg.helpList
  let addhelpCfg = diyCfg.helpCfg || sysCfg.helpCfg
  let helpname = e.msg.match(/标题.*/g)?.[0].replace(/标题/g, '').trim()
  addhelpCfg.title = helpname
  let helpList = `\nexport const helpList = ${JSON.stringify(addhelpList,null,'\t')}`
  let helpCfg = `export const helpCfg = ${JSON.stringify(addhelpCfg ,null,'\t')}`
  let add = helpCfg + helpList
  fs.writeFileSync(this.path,(add),'utf-8')
  e.reply('修改成功')
  } catch (err) {
    logger.warn(err)
    e.reply('修改失败')
  }
 }
}



